package com.jiantong.work.impl.service;

import org.springframework.stereotype.Service;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.impl.dao.ButcherReportsFormDao;
import com.jiantong.work.model.ButcherReportsForm;
import com.jiantong.work.service.ButcherReportsFormReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ButcherReportsFormReadServiceImpl implements ButcherReportsFormReadService{

    private final ButcherReportsFormDao butcherReportsFormDao;

    @Autowired
    public ButcherReportsFormReadServiceImpl(ButcherReportsFormDao butcherReportsFormDao) {
        this.butcherReportsFormDao = butcherReportsFormDao;
    }

    @Override
    public Response<ButcherReportsForm> findButcherReportsFormById(Long id) {
        try{
            ButcherReportsForm butcherReportsForm = butcherReportsFormDao.findById(id);
            if(butcherReportsForm == null){
                log.error("failed.to.find.butcher.report.form.by.id={}",id);
                return Response.fail("butcher.report.form.id.not.found");
            }
            return Response.ok(butcherReportsForm);
        }catch (Exception e){
            log.error("failed to find butcherReportsForm(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("butcherReportsForm.find.fail");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findSumGroupByProductType(Date start, Date end, List<Long> butcherFactoryIds) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            if(start != null){
                criteria.put("start",start);
            }
            if(end != null){
                criteria.put("end",end);
            }
            if(butcherFactoryIds != null){
                criteria.put("butcherFactoryIds",butcherFactoryIds);
            }
            List<Map<String , BigDecimal>> list = butcherReportsFormDao.findSumGroupByProductType(criteria);
            if(list == null){
                log.error("Sum(butcherFactoryIds={}) is not found", butcherFactoryIds);
                return Response.fail("Sum.not.found");
            }
            List<Map<String,Object>> list1 = new ArrayList<>();
            BigDecimal quantitySum = new BigDecimal(0);
            BigDecimal qualifiedQuantitySum = new BigDecimal(0);
            BigDecimal unqualifiedQuantitySum = new BigDecimal(0);
            BigDecimal beforeSlaughterQualifiedQuantitySum = new BigDecimal(0);
            BigDecimal beforeSlaughterUnqualifiedQuantitySum = new BigDecimal(0);
            BigDecimal syncQualifiedQuantitySum = new BigDecimal(0);
            float syncUnqualifiedQuantitySum = 0f;
            for (Map<String, BigDecimal> aList : list) {
                criteria.put("productType",aList.get("productType"));
                float syncUnqualifiedQuantitySum0 = 0f;
                syncUnqualifiedQuantitySum0 = butcherReportsFormDao.findSumSyncUnqualifiedQuantity(criteria);
                quantitySum = quantitySum.add(aList.get("quantity"));
                qualifiedQuantitySum = qualifiedQuantitySum.add(aList.get("qualifiedQuantity"));
                unqualifiedQuantitySum = unqualifiedQuantitySum.add(aList.get("unqualifiedQuantity"));
                beforeSlaughterQualifiedQuantitySum = beforeSlaughterQualifiedQuantitySum.add(aList.get("beforeSlaughterQualifiedQuantity"));
                beforeSlaughterUnqualifiedQuantitySum = beforeSlaughterUnqualifiedQuantitySum.add(aList.get("beforeSlaughterUnqualifiedQuantity"));
                syncQualifiedQuantitySum = syncQualifiedQuantitySum.add(aList.get("syncQualifiedQuantity"));
                syncUnqualifiedQuantitySum = syncUnqualifiedQuantitySum + syncUnqualifiedQuantitySum0;
                Map<String , Object> map1 = Maps.newHashMap();
                map1.put("productType",aList.get("productType"));
                map1.put("quantity",aList.get("quantity"));
                map1.put("qualifiedQuantity",aList.get("qualifiedQuantity"));
                map1.put("unqualifiedQuantity",aList.get("unqualifiedQuantity"));
                map1.put("beforeSlaughterQualifiedQuantity",aList.get("beforeSlaughterQualifiedQuantity"));
                map1.put("beforeSlaughterUnqualifiedQuantity",aList.get("beforeSlaughterUnqualifiedQuantity"));
                map1.put("syncQualifiedQuantity",aList.get("syncQualifiedQuantity"));
                map1.put("syncUnqualifiedQuantity",syncUnqualifiedQuantitySum);
                list1.add(map1);
            }
            Map<String , Object> map = Maps.newHashMap();
            map.put("productType",new BigDecimal(10000));
            map.put("quantity",quantitySum);
            map.put("qualifiedQuantity",qualifiedQuantitySum);
            map.put("unqualifiedQuantity",unqualifiedQuantitySum);
            map.put("beforeSlaughterQualifiedQuantity",beforeSlaughterQualifiedQuantitySum);
            map.put("beforeSlaughterUnqualifiedQuantity",beforeSlaughterUnqualifiedQuantitySum);
            map.put("syncQualifiedQuantity",syncQualifiedQuantitySum);
            map.put("syncUnqualifiedQuantity",syncUnqualifiedQuantitySum);
            list1.add(map);
            return Response.ok(list1);
        }catch (Exception e){
            log.error("failed to find Sum(applyReportIds={}), cause:{}", butcherFactoryIds, Throwables.getStackTraceAsString(e));
            return Response.fail("Sum.find.fail");
        }
    }

    @Override
    public Response<Paging<ButcherReportsForm>> pingButcherReportForm(Integer pageNo, Integer pageSize, Date start, Date end, List<Long> butcherFactoryIds, Integer productType) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            if(start != null){
                criteria.put("start",start);
            }
            if(end != null){
                criteria.put("end",end);
            }
            if(butcherFactoryIds != null){
                criteria.put("butcherFactoryIds",butcherFactoryIds);
            }
            if(productType != null){
                criteria.put("productType",productType);
            }
            return Response.ok(butcherReportsFormDao.butcherReportsFormPaging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherReportsForm, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("ButcherReportsForm.paging.fail");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findHarmlessSumGroupByProductType(Date start, Date end, List<Long> butcherFactoryIds) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            if(start != null){
                criteria.put("start",start);
            }
            if(end != null){
                criteria.put("end",end);
            }
            if(butcherFactoryIds != null){
                criteria.put("butcherFactoryIds",butcherFactoryIds);
            }
            List<Map<String , BigDecimal>> list = butcherReportsFormDao.findHarmlessSumGroupByProductType(criteria);
            if(list == null){
                log.error("Sum(butcherFactoryIds={}) is not found", butcherFactoryIds);
                return Response.fail("Sum.not.found");
            }
            List<Map<String , Object>> list1 = new ArrayList<>();
            BigDecimal unqualifiedQuantitySum = new BigDecimal(0);
            BigDecimal beforeSlaughterUnqualifiedQuantitySum = new BigDecimal(0);
            float syncUnqualifiedQuantitySum = 0f;
            for (Map<String, BigDecimal> aList : list) {
                criteria.put("productType",aList.get("productType"));
                float syncUnqualifiedQuantitySum0 = 0f;
                syncUnqualifiedQuantitySum0 = butcherReportsFormDao.syncUnqualifiedQuantitySum(criteria);
                unqualifiedQuantitySum = unqualifiedQuantitySum.add(aList.get("unqualifiedQuantity"));
                beforeSlaughterUnqualifiedQuantitySum = beforeSlaughterUnqualifiedQuantitySum.add(aList.get("beforeSlaughterUnqualifiedQuantity"));
                syncUnqualifiedQuantitySum = syncUnqualifiedQuantitySum + syncUnqualifiedQuantitySum0;
                Map<String , Object> map1 = Maps.newHashMap();
                map1.put("productType",aList.get("productType"));
                map1.put("unqualifiedQuantity",aList.get("unqualifiedQuantity"));
                map1.put("beforeSlaughterUnqualifiedQuantity",aList.get("beforeSlaughterUnqualifiedQuantity"));
                map1.put("syncUnqualifiedQuantity",syncUnqualifiedQuantitySum0);
                list1.add(map1);
            }
            Map<String , Object> map = Maps.newHashMap();
            map.put("productType",new BigDecimal(10000));
            map.put("unqualifiedQuantity",unqualifiedQuantitySum);
            map.put("beforeSlaughterUnqualifiedQuantity",beforeSlaughterUnqualifiedQuantitySum);
            map.put("syncUnqualifiedQuantity",syncUnqualifiedQuantitySum);
            list1.add(map);
            return Response.ok(list1);
        }catch (Exception e){
            log.error("failed to find harmless Sum(applyReportIds={}), cause:{}", butcherFactoryIds, Throwables.getStackTraceAsString(e));
            return Response.fail("harmless.Sum.find.fail");
        }
    }

    @Override
    public Response<Paging<ButcherReportsForm>> pagingButcherHarmlessForm(Integer pageNo, Integer pageSize, Date start, Date end, List<Long> butcherFactoryIds, Integer productType) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            if(start != null){
                criteria.put("start",start);
            }
            if(end != null){
                criteria.put("end",end);
            }
            if(butcherFactoryIds != null){
                criteria.put("butcherFactoryIds",butcherFactoryIds);
            }
            if(productType != null){
                criteria.put("productType",productType);
            }
            return Response.ok(butcherReportsFormDao.pagingButcherHarmlessForm(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging harmless ButcherReportsForm, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("harmless ButcherReportsForm.paging.fail");
        }
    }

    @Override
    public Response<Paging<ButcherReportsForm>> pagingCommon(Integer pageNo, Integer pageSize, Map criteria) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(butcherReportsFormDao.pagingCommon(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging harmless ButcherReportsForm, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("harmless ButcherReportsForm.paging.fail");
        }
    }
    /**
     * 屠宰检疫总体情况
     * @return
     */
    @Override
    public Response<List<Map>> tzjy(Map criteria) {
        try {
            return Response.ok(butcherReportsFormDao.tzjy(criteria));
        } catch (Exception e) {
            log.error("failed to find breedReportsForm(id={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("breedReportsForm.find.fail");
        }
    }
    /**
     * 本地入场数
     * @return
     */
    @Override
    public Response<Integer> local(Map criteria) {
        try {
            return Response.ok(butcherReportsFormDao.local(criteria));
        } catch (Exception e) {
            log.error("failed to find breedReportsForm(id={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("breedReportsForm.find.fail");
        }
    }
    /**
     * 屠宰来源分布
     * @param criteria
     * @return
     */
    @Override
    public Response<List<Map>> localbutcher(Map criteria) {
        try {
            return Response.ok(butcherReportsFormDao.localbutcher(criteria));
        } catch (Exception e) {
            log.error("failed to find breedReportsForm(id={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("breedReportsForm.find.fail");
        }
    }
    @Override
    public Response<ButcherReportsForm> findByReportId(Long reportId){
        try {
            return Response.ok(butcherReportsFormDao.findByReportId(reportId));
        } catch (Exception e) {
            log.error("failed to find breedReportsForm(id={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("breedReportsForm.find.fail");
        }
    }

    @Override
    public Response<Paging<ButcherReportsForm>> pagingButcherReportsFormForWeb(Integer page, Integer rows, Map<String, Object> criteria) {
        try {
            PageInfo pageInfo = new PageInfo(page, rows);
            return Response.ok(butcherReportsFormDao.butcherReportsFormPaging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to paging BreedReportsForm, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("BreedReportsForm.paging.fail");
        }
    }

    @Override
    public Response<List<ButcherReportsForm>> findListForWeb(Map<String, Object> map) {
        try {
            return Response.ok(butcherReportsFormDao.findListForWeb(map));
        } catch (Exception e) {
            log.error("failed to paging BreedReportsForm, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("BreedReportsForm.paging.fail");
        }
    }
}
